﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Yek.BI
{
    public partial class BIAutoCreateControl : UserControl
    {
        public BIAutoCreateControl()
        {
            InitializeComponent();
            using (var stream = Application.GetResourceStream(new Uri("/Yek.BI;component/greentree-iphone-1.json", UriKind.Relative)).Stream)
            {
                using (System.IO.StreamReader reader = new StreamReader(stream))
                {
                    string str = reader.ReadToEnd();
                    p = G.SL.Utils.SerializerHelp.JsonDeserialize<Data.BIData>(str);
                    classList.ItemsSource = p.Events.OrderBy(c => c.ClassName);
                }
            }

        }
        private const string secret = "YEKBIJLSKDJFIEJEKDMFDKFJSIJFKSDJFKSJDIFOEJKSJI4568SDFW3UI34U2121S2SDFKLLJU3PWERWRL";
        Data.BIData p;
        private void btnloadjsonurlfile_Click(object sender, RoutedEventArgs e)
        {
            string osName = "iphone";

            string signstring = txtProductCode.Text + osName + secret;
            string sign = System.Security.Cryptography.MD5.GetHashString(signstring).ToLower();
            string url = string.Format("http://bi.yekmob.com/index.php/admin/act-md-child-get/index/productCode/{0}/osName/{1}/sign/{2}", txtProductCode.Text, osName, sign);
            G.SL.Net.HttpGet get = new G.SL.Net.HttpGet(url);
            get.Request((staus, str) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    if (staus == null)
                    {
                        try
                        {
                            Data.RequestResponse response = G.SL.Utils.SerializerHelp.JsonDeserialize<Data.RequestResponse>(str);
                            if (response.Status == 0)
                            {
                                p = response.Data;
                                classList.ItemsSource = p.Events.OrderBy(c => c.ClassName);
                            }
                            else if (response.Status == -1)
                            {
                                MessageBox.Show(response.Msg);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("请检查product是否有效:" + ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("请检查product是否有效");
                    }
                });
            });
            //OpenFileDialog open = new OpenFileDialog();
            //if (open.ShowDialog() == true)
            //{
            //    try
            //    {
            //        using (var fileStream = open.File.Open(System.IO.FileMode.Open, System.IO.FileAccess.Read))
            //        {
            //            try
            //            {
            //                p = G.SL.Utils.SerializerHelp.JsonDeserialize<Data.BIData>(fileStream);
            //                classList.ItemsSource = p.Events.OrderBy(c => c.ClassName);
            //            }
            //            catch (Exception ex)
            //            {
            //                MessageBox.Show("JSOn文件出错，请使用正确的json文件！\r\n" + ex.Message);
            //            }
            //        }
            //    }
            //    catch (FieldAccessException ex)
            //    {
            //        MessageBox.Show("只能读取文档下面的文件！");
            //    }
            //}
        }

        private void save(object sender, RoutedEventArgs e)
        {
            if (p == null)
            {
                MessageBox.Show("请先加载json文件");
                return;
            }
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\BIGenerat\\" + DateTime.Now.ToString("yyyyMMddhhmmss");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            foreach (var ev in p.Events)
            {
                var filePath = path + "\\" + ev.FileName;
                using (var f = File.CreateText(filePath + ".cs"))
                {
                    f.Write(ev.WinPhoneFile);
                    f.Close();
                }
                //using (var f = File.CreateText(filePath + ".m"))
                //{
                //    f.Write(ev.MFile);
                //    f.Close();
                //}
            }

            //using (var f = File.CreateText(path + "\\bi_events.h"))
            //{
            //    f.Write(p.HFile);
            //    f.Close();
            //}

            SaveSuccessChildWindow s = new SaveSuccessChildWindow(path);
            s.Show();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == true)
            {
                try
                {
                    using (var f = open.File.OpenText())
                    {
                        p = G.SL.Utils.SerializerHelp.JsonDeserialize<Data.BIData>(f.ReadToEnd());
                        classList.ItemsSource = p.Events.OrderBy(c => c.ClassName);
                    }
                }
                catch
                {
                    MessageBox.Show("打开失败");
                }
            }
        }
    }
}
